package com.carpedil.sm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carpedil.sm.dao.UserMapper;
import com.carpedil.sm.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

/** @author Administrator */
@Component("userService")
public class UserServiceImpl implements UserService {
  @Qualifier("userMapper")
  @Autowired
  UserMapper userMapper;

  @Override
  public boolean insertUser(User user) {
    int i = userMapper.insert(user);
    return i > 0;
  }

  @Override
  public boolean deleteUserById(Integer id) {
    return userMapper.deleteById(id) > 0;
  }

  @Override
  public boolean updateUser(User user) {
    return userMapper.updateById(user) > 0;
  }

  @Override
  public List<User> getUsersByCondition(Integer id, String name, String nickName) {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper
        .eq("id", id)
        .and(i -> i.like("user_name", name))
        .and(i -> i.like("nick_name", nickName));
    return userMapper.selectList(wrapper);
  }

  @Override
  public IPage<User> getUsersByPage(Integer pageNo, Integer pageSize) {
    return userMapper.selectPage(new Page<>(pageNo, pageSize), null);
  }

  @Override
  public IPage<User> getUsersByPageParam(Integer pageNo, Integer pageSize, Integer roleId) {
    Page<User> page = new Page<>(pageNo, pageSize);
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("fk_role_id", roleId);
    return userMapper.getUsersByPageParam(page, wrapper);
  }
}
